package com.atguigu.srb.core.controller.api;

import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.exception.Assert;
import com.atguigu.srb.core.exception.BusinessException;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.pojo.query.UserInfoQuery;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.result.R;
import com.atguigu.srb.core.result.ResponseEnum;
import com.atguigu.srb.core.service.UserInfoService;
import com.atguigu.srb.core.util.RegexValidateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

//@CrossOrigin
@RestController
@Api(tags = "用户信息管理")
@RequestMapping("/api/core/userInfo")
public class WebUserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private RedisTemplate redisTemplate;

    // 发送短信时校验手机号是否注册
    @GetMapping("/checkMobile/{mobile}")
    public Boolean checkMobile(@PathVariable("mobile")String mobile){
        // 校验参数
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(mobile),"mobile",mobile);
        // 调用查询方法
        UserInfo userInfo = userInfoService.getOne(queryWrapper);
        return !ObjectUtils.isEmpty(userInfo);

    }



    @PostMapping("/login")
    @ApiOperation(value = "会员登录")
    public R login(@RequestBody @ApiParam(value = "会员信息", required = true) LoginVO loginVO,HttpServletRequest request) {
        // 获取登录的参数
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();

        // 参数校验
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);

        // req获取请求ip
        String ip = request.getRemoteAddr();

        // 调用业务层的登录方法
        UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);

        // 返回结果集
        return R.ok().data("userInfo", userInfoVO);
    }


    @ApiOperation(value = "用户会员注册检查token是否正确")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = null;

        token = request.getHeader("token");
        boolean res = JwtUtils.checkToken(token);
        if (res) {
            return R.ok();
        } else {
            return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);  // 没有登录
        }

    }


    @ApiOperation(value = "用户会员注册信息")
    @PostMapping(value = "/register")
    public R register(@RequestBody
                      @ApiParam(value = "用户信息", required = true)
                              RegisterVO registerVO) {
        String mobile = null;
        String password = null;
        String code = null;
        // 获取参数信息
        mobile = registerVO.getMobile();
        password = registerVO.getPassword();
        code = registerVO.getCode();
        // 前端参数校验
        // MOBILE_NULL_ERROR(-202, "手机号码不能为空"),
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        //    MOBILE_ERROR(-203, "手机号码不正确"),
        Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
        //    PASSWORD_NULL_ERROR(204, "密码不能为空"),
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        //    CODE_NULL_ERROR(205, "验证码不能为空"),
        Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);

        // 校验验证码,从redis缓存中获取验证码
        String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
        System.out.println("codeGen = " + codeGen);
        //    CODE_ERROR(206, "验证码错误"),
        Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);


        // 调用注册方法进行用户注册
        userInfoService.register(registerVO);
        // 返回结果
        return R.ok();

    }

}
